package com.zuikaku.utils;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

public class SMTPEmail {
    /**
     * SMTP服务器地址
     */
    private static String smtpHost;

    /**
     * 发信地址域
     */
    private static String domain;

    /**
     * 发信邮箱
     */
    private static String senderEmail;

    /**
     * 发信邮箱密码
     */
    private static String senderPassword;

    private static Session session;


    static {


        //读取配置文件
        ResourceBundle rb = ResourceBundle.getBundle("emailConfig");
        smtpHost = rb.getString("email.smtpHost");
        domain = rb.getString("email.domain");
        senderEmail = rb.getString("email.senderEmail");
        senderPassword = rb.getString("email.senderPassword");
        //设置全局变量mail相关为utf8编码
        System.setProperty("mail.mime.charset","UTF-8");
        //初始化邮件发送设置
        Properties properties = new Properties();
        properties.setProperty("mail.host", smtpHost);
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "true");
        //部署到公网,邮件发送使用465端口 ，默认25端口被封禁的。使用ssl模式
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.socketFactory.port", "465");
        session = Session.getInstance(properties);
        session.setDebug(true);
    }


    /**
     * 创建邮件(可带附件)
     *
     * @param session
     * @param receiverEmail
     * @param emailTitle
     * @param emailContent
     * @return
     * @throws Exception
     */
    private static Message createMessage(Session session, String receiverEmail, String emailTitle, String emailContent, List<File> aliasFileList) throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指定邮件发件人
        message.setFrom(new InternetAddress(domain));
        //指明邮件收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmail));
        //设置邮件标题
        message.setSubject(emailTitle);
        //设置内容
        MimeBodyPart textContent = new MimeBodyPart();
        textContent.setContent(emailContent, "text/html;charset=UTF-8");
        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(textContent);
        multipart.setSubType("mixed");
        //设置附件
        if (aliasFileList != null && aliasFileList.size() != 0) {


            for (File file : aliasFileList
            ) {
                MimeBodyPart attach = new MimeBodyPart();
                DataHandler dh = new DataHandler(new FileDataSource(file));
                attach.setDataHandler(dh);
                attach.setFileName(dh.getName());//设置附件名字
                multipart.addBodyPart(attach);
            }
        }
        //设置入message实体
        message.setContent(multipart);
        return message;
    }




    /**
     * 发送邮件(带附件)
     *
     * @param receiverEmailAddress
     * @param emailTitle
     * @param emailContent
     * @throws Exception
     */
    public static boolean sendEmail(String receiverEmailAddress, String emailTitle, String emailContent, List<File> aliasFileList) {
        try {
            Transport transport = session.getTransport();
            //发件者邮箱
            transport.connect(smtpHost, senderEmail, senderPassword);
            //创建邮件
            Message message = createMessage(session, receiverEmailAddress, emailTitle, emailContent, aliasFileList);
            //发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            //关闭流
            transport.close();
            return true;
        } catch (Exception e) {
            System.out.println("发送邮件时出现异常，异常信息为" + e);
            return false;
        }
    }

}
